<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * @author: Nguyen Van Nam
 * @Goal: Video class
 * @license: Nam & Thien
 * @contact: nguyennamcdt1@gmail.com
 * 
 */
require_once 'connect.php';
$action1 ='';
if(isset($_GET["todo"]))
    $action1 = $_REQUEST["todo"];
switch ($action1) {
    case 'showForm':        
        echo Video::getVideoForm(0,'video_add_action.php','post');
        break;
    case 'loadvideo':
        $recordid = htmlentities($_POST['recordid'], ENT_QUOTES);
        if(!empty($recordid)) {
            $record = Video::load($recordid);
            if(!empty($record)) {
                echo '{
                        "video_id" :"'.$recordid.'",
                        "video_url":"'.$video_url.'",
                        "video_title":"'.$video_title.'",
                        "video_bplay":"'.$video_bplay.'"
                    }';
            }
        }
        break;
}
        
class Video {
    public $video_id;
    public $video_url;
    public $video_title;
    public $video_bplay;
        
    function __construct($video_id = 0, $video_url="", $video_title="", $video_bplay="") {
       //something here;
       $this->video_id = $video_id;
       $this->video_url = $video_url;
       $this->video_title = $video_title;
       $this->video_bplay = $video_bplay;
    }
    
    function getVideoForm($video_id=0, $action="", $method="post" ) {       
        
        if( (!empty($video_id)) && (is_numeric($video_id)) ){ 
            $video = Video::load($videoid); 
        } else {
          $video = new Video(); 
        }
        $form = '<form name="video" action="'.$action.'" method="'.$method.'">
            <fieldset class="videoForm" width="680px">
                <legend><b>VideoForm</b></legend>
            <table align="center" width="650"  border="1" cellspacing="0" cellpadding="0">
                <tr>                    
                    <th align="right" >YouTube Url: </th>
                    <td><input type="text" name="video_url" size="88" class="text" value="'.$video->video_url.'"/></td>
                </tr>
                
                <tr>
                    <th align="right" >Video Title: </th>
                    <td><input type="text" name="video_title" class="text"size="88" value="'.$video->video_title.'"/></td>
                </tr>
                
                <tr>
                    <th align="right">Show:</th>
                    <td>
                        <select name="video_bplay" id="video_bplay">
                            <option value="1">Enabled</option>;
                            <option value="0">Disabled</option>;                                
                        </select>
                    </td>
                </tr>

                <tr>
                    <input type="hidden" name="video_id" value="'.$video->video_id.'"/>
                    <input type="hidden" name="todo" value=savevideo"/>
                    <td align="right"><input type="submit" value="Publish"  align="right"/></td>
                    <td><input type="reset" value="Reset" align="left"></td>
                </tr>       
                </fieldset>
            </table>
        </form>';       
        return $form;        
    }
    
    
    public function getAdminList() { 
        $videolist = Video::getRecords();
        echo '<fieldset align="center">
                <legend><b><i>Article Manager</i></b></legend>
                <table align="center" width=850 cellspacing="0" cellpadding="1" border="1">
                    <tr>
                        <th scope="col">ID</th>
                        <th scope="col">Title</th>
                        <th scope="col">Url</th>
                        <th scope="col">Statement</th>
                        <th scope="col">Edit</th>
                        <th scope="col">Delete</th>
                    </tr><tr>';
         if(!empty($videolist)) {
            foreach ( $videolist as $record ) {
                echo '<tr>';
                echo'<td align="center">'.$record['video_id'].'</td>';
                echo'<td align="center"><a href="../index_test.php?v='.$record['video_id'].'">'.$record['video_title'].'</a></td>';
                echo'<td align="center">'.$record['video_url'].'</td>';
                echo'<td align="center">'.$record['video_bplay'].'</td>';                
                echo'<td align="center"><a href="article_edit.php?video_id='.$record['video_title'].'">'.'<img src="../images/icon-48-article-edit.png" height="25" width="25" border="0">'.'</a></td>';                
                echo'<td align="center"><a href="article_delete.php?video_id=">'.'<img src="../images/delete.png" height="25" width="25" border="0">'.'</a></td>';
                echo'</tr>';
            }
            echo '</tr>
                  </fieldset>            
                </table>';        
        }
    }


    function getAdminSite()
    {
        if(!empty($_POST)) {
            Video::PostManager();
        }
        echo '<div id="adminwrap">
                <div id="addrecordwrap">
                     <input type="button" name="showForm" value="Add new video" onclick="showHint()" />
                </div>
                <div id="recordform">';       
        
        echo '</div>
            <div id="recordlist">';
        echo Video::getAdminList();            
        
        echo   '</div>';
        echo'
            <div id="publicside">
            <h3>Public side : Your List Of Videos</h3>';
        echo Video::getThumbnailList();
        echo'</div>';
        echo '</div>';
        
    }
    
    function load($id)
    {
        $sql = "select * from video_manager where video_id = $id LIMIT 1";
        $video = new Video();
        $sql = mysql_real_escape_string($sql);
        $req = mysql_query($sql) or die ('Error');
        while($data = mysql_fetch_assoc($req)) {
            $video->video_id = $data['video_id'];
            $video->video_url = $data['video_url'];
            $video->video_title = $data['video_title'];
            $video->video_bplay = $data['video_bplay'];            
        }
        return $video;
    }
    
    function save()
    {        
        $video_id = htmlentities($_POST['video_id'], ENT_QUOTES);
        $video_url = htmlentities($_POST['video_url'], ENT_QUOTES);
        $video_title = htmlentities($_POST['video_title'], ENT_QUOTES);
        $play = htmlentities($_POST['video_bplay'], ENT_QUOTES);
        if(!empty ($video_url)){
            if(!$video_id) {
                $sql = "insert into video_manager(video_url, video_title, video_bplay) values ('$video_url','$video_title', '$play')";
                if(mysql_query($sql)) {
                    return mysql_insert_id();
                } else {
                    die('SQL Error!<br>'.$sql.'<br>'.mysql_error());
                    return false;
                }
            }
        } else {
            //update 
            $modify='';
            if(!empty ($video_url)) {
                $modify.="`video_url` = '$video_id',";            
            }
            if(!empty($video_title)) { 
                $modifs.=" `video_title` = '$video_title',"; }
            $sql = "UPDATE video_manager SET ".substr($modifs,0,-1)." WHERE video_id = $video_id";
            if(mysql_query($sql)) { 
                return $video_id;                 
            } else { 
                die("SQL Error!<br>".$sql."<br>".mysql_error()); 
                return false;                 
            }
        } 
    }
    
    function getRecords()
    {
        $sql = 'select * from video_manager';
        $result = array();
        $req = mysql_query($sql) or die ('SQL query error');
        while ($data = mysql_fetch_array($req)) {
            $result[] = $data;
        }            
        return $result;
    }
    
    public function getThumbnailList()
    {
        $thumbdom="<ul>"; $playerdom="";

        //Get The videos from DB
        $videolist = Video::getrecords();

        //Loop Through Them
        if(!empty($videolist)){
            foreach($videolist as $video){

              //Get YouTube Id
              $youtubeid = str_replace("watch?v=","",end(explode("/",$video['video_url'])));
              $youtubeid = reset(explode("&",$youtubeid));

              //Youtube video File + Youtube video Thumbnail
              $thumb = "http://img.youtube.com/vi/$youtubeid/default.jpg";
              $file = "http://www.youtube.com/v/$youtubeid";

              //Build Up your list of video
              $thumbdom.='<li>
              <a >
                <img src="'.$thumb.'" alt="'.$video['video_title'].'" />
              </a>
              </li>';
              
              //Also adds the corresponding player for the video
              $playerdom.='<div class="video_player_wrap" id="video'.$video['video_id'].'">
                            <embed style="width:400px;height:350px" src="'.$file.'&autoplay=1&hl=en&fs=1
                            &rel=0&color1=0x3a3a3a&color2=0x999999" 
                            type="application/x-shockwave-flash" allowscriptaccess="always" 
                            allowfullscreen="true" width="320" height="265">
                            </embed>
                          </div>'."\n";
            }        
        }
        $thumbdom.="</ul>";
        $dom='<div id="thumbnailscontainer">'.$thumbdom.'</div>
        <div id="playercontainer">'.$playerdom.'</div>';
        echo $playerdom;
        return $dom;
    }

    public function PostManager() {
        $action = htmlentities($_POST['todo'], ENT_QUOTES);
        switch ($action) {
            case 'savevideo':
                Video::save();
                break;
            case 'loadvideo':
                $recordid = htmlentities($_POST['recordid'], ENT_QUOTES);
                if(!empty($recordid)) {
                    $record = Video::load($recordid);
                    if(!empty($record)) {
                        echo '{
                                "video_id" :"'.$recordid.'",
                                "video_url":"'.$video_url.'",
                                "video_title":"'.$video_title.'",
                                "video_bplay":"'.$video_bplay.'"
                            }';
                    }
                }
                break;
        }
    }
}
?>
